package com.wanpinghui.supplier.common;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.wanpinghui.supplier.R;

import org.xutils.x;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/***
 * 功能描述:
 * 作者:qiujialiu
 * 时间:2016/12/16
 * 版本:
 ***/
public abstract class BaseFragment extends Fragment {
    protected Context mContext;
    private boolean injected = false;
    private CompositeSubscription mSubscriptions;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        injected = true;
        mContext = getActivity();
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!injected) {
            x.view().inject(this, this.getView());
        }
        init();
    }

    public abstract void init();


    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        getActivity().overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);
    }

    protected void addSub(Subscription sb) {
        if (mSubscriptions == null) {
            mSubscriptions = new CompositeSubscription();
        }
        mSubscriptions.add(sb);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSubscriptions != null && !mSubscriptions.isUnsubscribed()) {
            mSubscriptions.unsubscribe();
        }
    }
}
